﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.IO.Pipes;
using System.Xml;

namespace SetPassword
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnEnter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "OsmEncryptionPipe", PipeDirection.InOut))
                {
                    pipeClient.Connect(100);
                    WriteToPipe(pipeClient, "<SetPassword>" + txtPassword.Password + "</SetPassword>");
                }
                Close();
            }
            catch (Exception err)
            {
                MessageBox.Show("Apparently the OsmEncryptionService is not running.\n" + err.Message);
            }
        }

        private void WriteToPipe( NamedPipeClientStream pipeClient, string value )
        {
            byte[] buffer = Encoding.ASCII.GetBytes(value);
            pipeClient.Write(buffer, 0, buffer.Length);
        }
    }
}
